home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / stut_src / progress.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  11KB  |  420 lines

  1. /*
  2.  * progress.c
  3.  *
  4.  * Purpose:
  5.  * --------
  6.  * Routines de gestion des progress-indicators
  7.  *
  8.  * History:
  9.  * --------
  10.  * 1993: fplanque: Created
  11.  */
  12.  
  13.  
  14.      #include "!OPTIONS.H"                /* Options de compilation */         
  15.     #define    THIS_FILE    "PROGRESS.C v1.00 - 03.95"
  16.           
  17.  
  18. /*
  19.  * System headers:
  20.  */
  21.     #include    <stdio.h>
  22.     #include    <string.h>                    /* strlen() etc.. */
  23.     #include    <stdlib.h>                    /* malloc etc... */
  24.     #include    <aes.h>                        /* header AES */
  25.    
  26.  
  27. /*
  28.  * Custom headers:
  29.  */
  30.     #include    "SPEC_PU.H"    
  31.     #include    "STKEYMAP.H"    
  32.     #include "STUT_ONE.RSC\STUT_3.H"                    /* noms des objets ds le ressource */
  33.  
  34.  
  35. /*
  36.  * ------------------------ PROTOTYPES -------------------------
  37.  */
  38.  
  39.  
  40. /*
  41.  * EXTernal prototypes:
  42.  */
  43.     /* 
  44.      * G‚n‚ral: 
  45.      */
  46.     extern    void    signale( char * inf );
  47.     extern    void    erreur_rsrc( void );
  48.     /* 
  49.      * Objects: 
  50.      */
  51.     extern    void    rsrc_color( OBJECT *tree );
  52.     extern    void    dlink_teptext ( OBJECT *objc_adr , const char *texte );
  53.     extern    void    rlink_teptext ( OBJECT *objc_adr , const char *texte );
  54.     extern    GRECT    * objc_xywh ( OBJECT *tree, int objc, GRECT *frame );
  55.     extern    void    objc_sel( OBJECT *tree, int obj );
  56.     extern    void    objc_dsel( OBJECT *tree, int obj );
  57.     extern    void    objc_clrsel(  OBJECT *tree, int obj );
  58.     /* 
  59.      * Sp‚cial: 
  60.      */
  61.     extern    void    grecttoarray( GRECT *rect, int *array );
  62.     extern    long    l_min( long a, long b);        /* Minimum de 2 long int */
  63.     extern    long    l_max( long a, long b);        /* Minimum de 2 long int */
  64.     extern    void    ping( void );
  65.  
  66.  
  67. /*
  68.  * ------------------------ VARIABLES -------------------------
  69.  */
  70.     
  71. /*
  72.  * External variables: 
  73.  */
  74.     /* 
  75.      * G‚n‚ral: 
  76.      */
  77.     extern    int        G_ev_mgpbuff[8];                /* Buffer d'‚venements GEM */
  78.  
  79.  
  80. /*
  81.  * Private variables: 
  82.  */
  83.     /* 
  84.      * Objets: 
  85.      */
  86.     static    OBJECT    *M_std_progress;
  87.     static    GRECT        M_progress_form_box;    /* Coord boite complŠte */
  88.     static    GRECT        M_prog_indicator_box;    /* Coord de la boite de l'indicateur */
  89.     static    GRECT        M_prog_cancel_box;        /* Coord du bouton Annuler */
  90.     static    int        M_prog_cancel_xy[4];    /* Coord en X1 y1 x2 y2 */
  91.     /* 
  92.      * Variables du progress en cours: 
  93.      */
  94.     static    BOOL        M_b_DontManageEnv = FALSE0;
  95.     static    BOOL        M_b_visible = FALSE0;        /* Le progress est-il visible? */     
  96.     static    int        M_curr_prog_width;        /* Largeur de progress courament affich‚ee */    
  97.     static    int        M_max_prog_width;        /* Largeur maximale */
  98.     static    long        M_curr_prog_length;        /* Longueur totale du progress en cours */
  99.     static    long        M_curr_slice_size;        /* Longueur d'une tranche avant mise … jour */
  100.     static    int        M_m1_state;                /* Etat du contr“le position souris par rapport au bouton annuler */
  101.  
  102. /*
  103.  * ------------------------ FUNCTIONS -------------------------
  104.  */
  105.  
  106.  
  107.  
  108. /*
  109.  * init_progress(-)
  110.  *
  111.  * Purpose:
  112.  * --------
  113.  * Initialisations
  114.  *
  115.  * History:
  116.  * --------
  117.  * fplanque: Created
  118.  */
  119. void    init_progress( void )
  120. {
  121.     /*
  122.      * Trouve adresses des arbres d'objets: 
  123.      */
  124.     if (rsrc_gaddr( R_TREE, PROGRESS, &M_std_progress) == 0)
  125.         erreur_rsrc();
  126.     rsrc_color( M_std_progress );        /* Fixe couleurs */
  127.  
  128.     /*
  129.      * Largeur de la boŒte de progress: 
  130.      */
  131.     M_max_prog_width = M_std_progress[ PROGBOX ] .ob_width;
  132.     
  133. }
  134.  
  135.  
  136.  
  137. /*
  138.  * start_progress(-)
  139.  *
  140.  * Purpose:
  141.  * --------
  142.  * Affichage de la boite de progression
  143.  *
  144.  * History:
  145.  * --------
  146.  * 1993: fplanque: Created
  147.  * 08.01.95: param b_DontManageEnv
  148.  * 30.01.95: prend taille de bloc minimale qu'on accepte
  149.  */
  150. long    start_progress(                             /* Out: Taille de bloc pour obtenir la fluidit‚ maximale */
  151.             const char *    title, 
  152.             const char *    info, 
  153.             long                 prog_length,
  154.             long                l_MinBlockSize,    /* In: Taille de bloc minimale qu'on accepte */
  155.             BOOL                b_DontManageEnv )    /* In: True si il ne faut pas g‚rer l'environnent, par ex si on a d‚j… un dialogue ouvert … l'‚cran */
  156.             
  157. {
  158.     /*
  159.      * Initialise la progression: 
  160.      */
  161.     M_b_DontManageEnv     = b_DontManageEnv;
  162.     M_curr_prog_length = prog_length;    /* Longueur de la progression */
  163.     M_m1_state = 0;    /* Par d‚faut, la souris n'est pas sur le bouton "Annuler" */
  164.  
  165.     /*
  166.      * Fixe textes: 
  167.      */
  168.     dlink_teptext ( &(M_std_progress[ PROGTITL ]) , title );    /* Titre */
  169.     rlink_teptext ( &(M_std_progress[ PROGINFO ]) , info );    /* Info */
  170.  
  171.     /*
  172.      * Reset progress bar: 
  173.      */
  174.     M_curr_prog_width = 0;        /* Largeur affich‚e: 0 */
  175.  
  176.     /*
  177.      * Centre formulaire: 
  178.      */
  179.     form_center( M_std_progress, 
  180.         &(M_progress_form_box .g_x), &(M_progress_form_box .g_y), &(M_progress_form_box .g_w), &(M_progress_form_box .g_h) );
  181.     
  182.     /*
  183.      * Sauve coordonn‚es de l'indicateur: 
  184.      */
  185.     objc_xywh( M_std_progress, PROGBOX, &M_prog_indicator_box );
  186.     objc_xywh( M_std_progress, PROGANNU, &M_prog_cancel_box );
  187.     grecttoarray( &M_prog_cancel_box, M_prog_cancel_xy );
  188.  
  189.     /*
  190.      * Souris: 
  191.      */
  192.     graf_mouse( BUSYBEE, 0);    /* souris: Abeille */
  193.  
  194.     /*
  195.      * On d‚clare cette ouverture … l'AES: 
  196.      */
  197.     if( b_DontManageEnv == FALSE0 )
  198.     {
  199.         /* App prend en charge souris */
  200.         WIND_UPDATE_BEG_MCTRL
  201.  
  202.         form_dial( FMD_START, 0, 0, 0, 0,
  203.             M_progress_form_box .g_x, M_progress_form_box .g_y, M_progress_form_box .g_w, M_progress_form_box .g_h );
  204.         /* printf("\033Y  F"); */
  205.     }
  206.     
  207.     /*
  208.      * Dessine objet: 
  209.      */
  210.     objc_draw( M_std_progress, 0, 2,     /* La profondeur choisie fait que l'on n'affiche pas la barre elle mˆme, juste son cadre! */
  211.         M_progress_form_box .g_x, M_progress_form_box .g_y, M_progress_form_box .g_w, M_progress_form_box .g_h );
  212.  
  213.     /*
  214.      * Progress est maintenant visible:
  215.      */
  216.     M_b_visible = TRUE_1;
  217.  
  218.     /*
  219.      * Calcule la longueur d'une tranche: 
  220.      * (Longueur aprŠs laquelle on recommande au processus appellant de faire un update,
  221.      * afin d'obtenir une fluidit‚ maximale 
  222.      */
  223.     M_curr_slice_size = l_max( M_curr_prog_length / M_max_prog_width, l_MinBlockSize );    /* Minimum l_MinBlockSize */
  224.     return     l_min( M_curr_slice_size, M_curr_prog_length );    /* Maximum calcul‚ pour ne pas d‚passer la fin */
  225. }
  226.  
  227.  
  228.  
  229.  
  230. /*
  231.  * update_progress(-)
  232.  *
  233.  * Purpose:
  234.  * --------
  235.  * Mise … jour de la progression
  236.  *
  237.  * History:
  238.  * --------
  239.  * 1993: fplanque: Created
  240.  * 28.09.94: gŠre la visibilit‚ de la boŒte
  241.  * 31.01.95: retourne taille block et non pas longueur totale
  242.  */
  243. long    update_progress( 
  244.             long prog_done )
  245. {
  246.     int    event;
  247.     int    mx, my, mstate, mnb_clicks;
  248.     int    key_state, key_ret;
  249.  
  250.     /*
  251.      * Calcule nlle longueur de la barre de progression: 
  252.      */
  253.     int    new_prog_width;
  254.     
  255.     if( M_curr_prog_length != 0 )
  256.     {
  257.         new_prog_width = (int) (M_max_prog_width * prog_done / M_curr_prog_length );
  258.     }
  259.     else
  260.     {
  261.         new_prog_width = M_max_prog_width;
  262.     }
  263.  
  264.     /*
  265.      * Contr“le si ‡a a boug‚: 
  266.      */
  267.     if ( new_prog_width > M_curr_prog_width )
  268.     {    /*
  269.          * Si le progress … avanc‚ 
  270.          * Fixe nlle longueur: 
  271.          */
  272.         M_std_progress[ PROGBAR ] .ob_width = new_prog_width;
  273.  
  274.         /*
  275.          * Affiche le bout en plus: 
  276.          */            
  277.         if( M_b_visible )
  278.         {
  279.             objc_draw( M_std_progress, PROGBAR, 1,     
  280.                 M_prog_indicator_box .g_x + M_curr_prog_width, M_prog_indicator_box .g_y, 
  281.                 new_prog_width -M_curr_prog_width +1, M_prog_indicator_box .g_h );
  282.         }
  283.         
  284.         /* 
  285.          * Nlle longueur courante: 
  286.          */
  287.         M_curr_prog_width = new_prog_width;
  288.     }
  289.  
  290.     /*
  291.      * Appel rapide de l'AES pour voir si l'utilisateur d‚sire annuler: 
  292.      */
  293.     event = evnt_multi( MU_TIMER|MU_M1|MU_BUTTON|MU_KEYBD,
  294.                                 1, 1, 1,
  295.                           M_m1_state, M_prog_cancel_box .g_x, M_prog_cancel_box .g_y, M_prog_cancel_box .g_w, M_prog_cancel_box .g_h,
  296.                           0, 0, 0, 0, 0,
  297.                           G_ev_mgpbuff,
  298.                           0, 0,
  299.                           &mx, &my, &mstate,
  300.                           &key_state, &key_ret,
  301.                           &mnb_clicks );
  302.  
  303.     /*
  304.      * Analyse r‚ponse AES: 
  305.      */
  306.     if ( (event & MU_M1) && M_b_visible )
  307.     {    /*
  308.           * Si la souris est entr‚e/sortie du rectangle: 
  309.           * et si la boite est affich‚e
  310.           */
  311.         if ( M_m1_state == 0 )
  312.         {    /*
  313.              * Si la souris est entr‚e: 
  314.              */
  315.             graf_mouse( ARROW, 0);    /* souris: FlŠche */
  316.             objc_sel( M_std_progress, PROGANNU );    /* S‚lectionne bouton */
  317.             M_m1_state = 1;    /* On va attendre que la souris ressorte */
  318.         }
  319.         else
  320.         {    /*
  321.               * Si la souris est sortie: 
  322.               */
  323.             graf_mouse( BUSYBEE, 0);    /* souris: Abeille */
  324.             objc_dsel( M_std_progress, PROGANNU );    /* D‚s‚lectionne bouton */
  325.             M_m1_state = 0;    /* On va attendre que la souris entre … nouveau */
  326.         }
  327.     }
  328.     
  329.     if ( (event & MU_BUTTON) &&  M_b_visible )
  330.     {    /*
  331.          * Si on a cliqu‚ et si form affich‚
  332.          * Contr“le si on a cliqu‚ … l'int‚rieur du bouton "Annuler": 
  333.          */
  334.         if (    mx >= M_prog_cancel_xy[ 0 ] && mx <= M_prog_cancel_xy[ 2 ]
  335.             &&    my >= M_prog_cancel_xy[ 1 ] && my <= M_prog_cancel_xy[ 3 ] )
  336.         {    /*
  337.              * Si on a annul‚: 
  338.              */
  339.             objc_sel( M_std_progress, PROGANNU );    /* S‚lectionne bouton */
  340.             return    TRUE_1;            /* On a interrompu l'action en cours... */
  341.         }
  342.     }
  343.         
  344.     if ( event & MU_KEYBD )
  345.     {
  346.         if ( key_ret == STSCAN_UNDO )
  347.         {    /*
  348.              * Si on a appuy‚ sur UNDO 
  349.              */
  350.             if( M_b_visible )
  351.             {
  352.                 objc_sel( M_std_progress, PROGANNU );    /* S‚lectionne bouton */
  353.             }
  354.             return    TRUE_1;            /* On a interrompu l'action en cours... */
  355.         }
  356.  
  357.     }
  358.  
  359.     /*
  360.      * On peut continuer l'affichage... 
  361.      * Calcule la taille de la prochaine tranche: 
  362.      * (Longueur aprŠs laquelle on recommande au processus appellant de faire un update, afin d'obtenir une fluidit‚ maximale)
  363.      */
  364.     return     l_min( M_curr_slice_size, M_curr_prog_length - prog_done );    /* Maximum calcul‚ pour ne pas d‚passer la fin */
  365. }
  366.  
  367.  
  368.  
  369.  
  370. /*
  371.  * end_progress(-)
  372.  *
  373.  * Purpose:
  374.  * --------
  375.  * Effa‡age de la boite de progression
  376.  *
  377.  * History:
  378.  * --------
  379.  * 1993: fplanque: Created
  380.  * 08.01.95: param pObj_BackgrdForm
  381.  */
  382. void    end_progress(
  383.             OBJECT    *    pObj_BackgrdForm )    /* In: Arbe … afficher en fond */
  384. {
  385.     /*
  386.      * Affiche le progress jusqu'au bout:
  387.      */
  388.     update_progress( M_curr_prog_length );
  389.  
  390.     if( pObj_BackgrdForm != NULL )
  391.     {    /* 
  392.           *    Red‚ssinne le fond: 
  393.           */
  394.         objc_draw( pObj_BackgrdForm, 0, 5, M_progress_form_box .g_x, M_progress_form_box .g_y, M_progress_form_box .g_w, M_progress_form_box .g_h );
  395.     }
  396.  
  397.     if( M_b_DontManageEnv == FALSE0 )
  398.     {    /*
  399.            * On efface le formulaire par l'AES: 
  400.            */
  401.         form_dial( FMD_FINISH, 0, 0, 0, 0,
  402.             M_progress_form_box .g_x, M_progress_form_box .g_y, M_progress_form_box .g_w, M_progress_form_box .g_h );
  403.         /* printf("\033Y  -"); */
  404.     
  405.         /* AES peut reprendre la souris */
  406.         WIND_UPDATE_END_MCTRL
  407.     }
  408.     
  409.     /*
  410.      * Progress est maintenant invisible:
  411.      */
  412.     M_b_visible = FALSE0;
  413.  
  414.     /*
  415.      * D‚s‚lectionne bouton Annuler 
  416.      */
  417.     objc_clrsel( M_std_progress, PROGANNU );
  418.  
  419. }
  420.